package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.admin.DishFlavorMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetMealDishMapper;
import com.sky.mapper.admin.SetMealMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SetMealDishMapper setMealDishMapper;

    @Override
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.addDish(dish);
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size()>0){
            flavors.forEach(flavor->{
                flavor.setDishId(dishId);
            });
            dishFlavorMapper.addDishFlavor(flavors);
        }
        cleanRedis("Dish" + dishDTO.getCategoryId());
    }

    @Override
    public PageResult findByPage(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<DishVO> dishes = dishMapper.findByPage(dto);
        return new PageResult(dishes.getTotal(),dishes.getResult());
    }

    @Override
    public void deleteByIds(List<Long> ids,Integer status) {
        if (dishMapper.selectCountByIdsAndStatus(ids, StatusConstant.ENABLE) > 0){
            throw new DeletionNotAllowedException( MessageConstant.DISH_ON_SALE);
        }
        //查询删除的菜品是否有被套餐关联的
        Integer count2= setMealDishMapper.selectCountByDishIds(ids);
        if(count2>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        dishMapper.deleteByIds(ids);
        dishFlavorMapper.deleteByIds(ids);
        cleanRedis("Dish-*");
    }

    @Override
    public DishVO findById(Long dishId) {
        DishVO dishVO = dishMapper.findById(dishId);
        List<DishFlavor> dishFlavors = dishFlavorMapper.findById(dishId);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //先删除原有口味的数据
        dishFlavorMapper.deleteByIds(Arrays.asList(dish.getId()));
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //添加口味
        flavors.forEach(flavor->{flavor.setDishId(dish.getId());});
        dishFlavorMapper.addDishFlavor(flavors);
        cleanRedis("Dish-*");
    }

    @Override
    public List<Dish> findByCategory(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.findByCategory(dish);

    }

    /**
     * 菜品起售停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .status(status)
                .id(id).build();
        dishMapper.update(dish);
        cleanRedis("Dish-*");
    }

    private void cleanRedis(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
